package tiepbm.app.connect.http;

import tiepbm.app.store.Common;
import tiepbm.app.util.Util;
import android.app.Activity;
import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;

public abstract class VolleyListener {

	private Response.Listener<String> success;
	private Response.ErrorListener error;
	private Activity activity;
	private Context appContext;

	/**
	 * 
	 * @param context
	 *            Use application context
	 */
	public VolleyListener(Activity acivity) {
		this.activity = acivity;
		appContext = activity.getApplicationContext();
		success = new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				onFinish();
				onSuccess(response);
			}
		};
		error = new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				onFinish();
				onError(error);
			}
		};
	}

	public Response.Listener<String> getSuccessListener() {
		return success;
	}

	public Response.ErrorListener getErrorListener() {
		return error;
	}

	public boolean start() {
		if (!Util.isNetworking(appContext)) {
			Util.showDialog(Common.CODE_ERROR_CONNECT, activity);
			return false;
		}
		onStart();
		return true;
	}

	public abstract void onStart();

	public abstract void onSuccess(String response);

	public abstract void onError(VolleyError error);

	public abstract void onFinish();

}
